// WorkerThreadManager.h
//
#ifdef MULTIQUEUE

#ifndef WORKERTHREADMANAGER_H
#define WORKERTHREADMANAGER_H

#include <vector>
#include <QVector>
#include <QThread>
#include <QQueue>
#include <QList>
#include <QMutex>

namespace IDS {
namespace IDEAS {
namespace OpenGLView {

class VObjectGL;
class WorkerThread;

class WorkerThreadManager : public QThread
{
public:
	enum TaskType
	{
		UPDATE_OBJECT,
		DELETE_OBJECT,
	};

	struct Task
	{
		std::vector<VObjectGL *> objects;
		double precision;
		TaskType operation;

		//Task( VObjectGL *o, double approx, TaskType op ) : object(o), precision(approx), operation(op) {} 
	};

protected:
	struct Request
	{
		VObjectGL *object;
		double precision;
	};

	QMutex mutex;
	QQueue<Request> updateQueue;
	QQueue<Request> removeQueue;
	QList<VObjectGL*> pending;
	QVector<WorkerThread*> threadPool;

	void run();

	void removeDeletedObjects();
	void deletePendingObjects();
	bool scheduleTask();
	int getFreeWorker();
	int getBusyWorker();

	const int MAX_WORKER_THREAD_NUMBER;
	bool updating;
	bool running;

public:
	WorkerThreadManager();
	~WorkerThreadManager();

	void addTask( const Task &t );

	// --------------------------------------------------------------------------------------------
	void start();
	void stop();

	bool isUpdating() {return updating;}
	void resetUpdating() {updating = false;}
};

}}}


#endif

#endif